package com.info.zhiduoduo.common.validator;

import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData;
import com.info.zhiduoduo.common.utils.IdentityUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * Excel导入字段校验
 *
 * @author tianyonghong
 * @create 2022/7/22
 */
public class ExcelImportValid {

    /**
     * Excel导入字段校验
     *
     * @param object 校验的JavaBean 其属性须有自定义注解
     * @author 阿落学Java
     */
    public static void valid(int index, Object object) {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            try {
                fieldValue = field.get(object);
            } catch (IllegalAccessException e) {
                throw new ExcelDataConvertException(index, 0, null, null, field.getAnnotation(ExcelValid.class).message());
            }

            //是否包含必填校验注解
            boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
            if (isExcelValid && Objects.isNull(fieldValue)) {
                throw new ExcelDataConvertException(index, 0, null, null, field.getAnnotation(ExcelValid.class).message());
            }

            //是否包含decimal类型注解
            boolean isExcelDecimalValid = field.isAnnotationPresent(ExcelDecimalValid.class);
            if (isExcelDecimalValid) {
                if (isBigDecimal(fieldValue.toString())) {
                    BigDecimal cellDecimal = new BigDecimal(fieldValue.toString()).setScale(2,BigDecimal.ROUND_HALF_UP);
                    try {
                        // 修改精确度后重新赋值
                        field.set(object,cellDecimal);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    BigDecimal min = new BigDecimal(field.getAnnotation(ExcelDecimalValid.class).min());
                    BigDecimal max = new BigDecimal(field.getAnnotation(ExcelDecimalValid.class).max());
                    if (cellDecimal.compareTo(min) <= 0 || cellDecimal.compareTo(max) > 0) {
                        throw new ExcelDataConvertException(index, 0, new CellData(fieldValue), null, field.getAnnotation(ExcelDecimalValid.class).message());
                    }
                } else {
                    throw new ExcelDataConvertException(index, 0, new CellData(fieldValue), null, "不是数字类型");
                }
            }

            //是否包含号码注解
            boolean isValidPhone = field.isAnnotationPresent(Phone.class);
            if(isValidPhone){
                if(fieldValue == null || !fieldValue.toString().matches("^1\\d{10}$")){
                    throw new ExcelDataConvertException(index, 0, new CellData(fieldValue), null, "手机号格式错误");
                }
            }

            //是否包含身份证注解
            boolean isValidIdCard = field.isAnnotationPresent(IdCard.class);
            if(isValidIdCard){
                if(!IdentityUtil.isLegalIdCard(fieldValue.toString())){
                    throw new ExcelDataConvertException(index, 0, new CellData(fieldValue), null, "身份证格式错误");
                }
            }
        }
    }

    private static boolean isBigDecimal(String decimal) {
        try {
            BigDecimal bd = new BigDecimal(decimal);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
